package com.mytest.test.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.mytest.test.mapper.MyInsertOrUpdateBatchMapper;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import java.util.Collection;
import java.util.List;

/**
 * @description:重写saveOrUpdateBatch方法
 * @author: chengxucai
 * @createDate: 2022/3/14
 */
public class BatchServiceImpl<M extends MyInsertOrUpdateBatchMapper<T>, T> extends ServiceImpl<M, T> {
    /**
     * 这快就调用了刚才自定义的mapper
     * @param entityList 传入集合
     * @param batchSize 条数
     * @return 返回成功与否
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize){
        Assert.notEmpty(entityList, "error: entityList must not be empty");
        List<T> objs = Lists.newArrayList(entityList);
        List<List<T>> partitions = Lists.partition(objs, batchSize);
        for(List<T> list:partitions) {
            this.baseMapper.mysqlInsertOrUpdateBath(list);
        }
        return true;
    }
}
